/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * All Rights Researved.
 *
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 *
 * @Original Author: jin Chen
 * @date: Oct 23, 2003$
 * @version: 1.0
 */
package edu.psu.geovista.app.dataloader.v1.components;

import edu.psu.geovista.app.dataloader.v1.config.model.TimeSeriesData;
import edu.psu.geovista.app.dataloader.v1.gui.DataSelectPane;
import edu.psu.geovista.app.dataloader.v1.gui.TSDataSelectPane;
import edu.psu.geovista.common.awt.event.dataloading.TSDataListener;
import edu.psu.geovista.ui.dataloader.common.FileLoadingPane;
import edu.psu.geovista.ui.dataloader.event.LoadDataEvent;
import edu.psu.geovista.ui.dataloader.event.LoadDataListener;

import javax.swing.border.TitledBorder;
import java.io.File;

public class TsDataLoader extends SingleDataLoader /*implements DataSetListener*/{
    public static final String TsDataFile="Time Series";
    public static final String TsMetaFile="TsMeta data";

     public static final String TsDataLabel="Time Series";
    public static final String TsMetaLabel="Metadata";

    public static final boolean DEBUG=false;
    public TsDataLoader(NCIDataSource dataSource) {
        super(dataSource);
        TitledBorder border = new TitledBorder("Time Series Data");

        border.setTitleColor(TEXT_COLOR );
        this.setBorder(border);
         //this.setBorder(new TitledBorder("Time Series Data"));
    }
    /**
     * set pcp data load based on project setting
     * @param datainfo
     */
    public void setDataInfo(TimeSeriesData datainfo){

          if (datainfo!=null) {
              super.setDataInfo(datainfo);
              //2. set time values  
            /* Properties tvs=ConfigureProcessor.getTimeValues(datainfo);
             ((TSDataListPane)dataList).setTimeValues(tvs);*/

        } else {
             this.setAllFileLoaderValues("");
        }
    }
     /**
     * put data information into datainfo
     * @param datainfo
     */
    public void fillProjectInfo(TimeSeriesData datainfo){

        super.fillProjectInfo(datainfo);
        /*TSDataListPane dtlist= (TSDataListPane) this.dataList;
        Properties tvs=dtlist.getTimeValues() ;
        ConfigureProcessor.setTimeValues(datainfo,tvs);*/
    }


    public void initComponents(){
        /**
         * It is not easy to set dataPath from outside since dataloader need to specify
         * name of FileLoaderPath(Time Series,TsMeta data...)
         */
        //ListPane
        this.dataAssembler.setDataReceiverType(edu.psu.geovista.app.dataloader.v1.components.DataAssembler.DataReceiver_TS );  ////It means the dataAssembler(and thus this dataloader) will send TS data
        dataList=new TSDataSelectPane(dataAssembler);
        setupListPane(dataList);

        //pathPane
        String[] names={TsDataFile,TsMetaFile};
        dataPathPane=new FileLoadingPane(names);
        String [] labels={TsDataLabel,TsMetaLabel};
        dataPathPane.setFileSrcLabels(labels);


        ldl=new LoadDataListener(){
             public void loadData(LoadDataEvent evt) {
                 /*FileLoadingPane flp=(FileLoadingPane) evt.getSource() ;
                 String obF=flp.getFileLoaderValue(TsDataFile);
                 String metaF=flp.getFileLoaderValue(TsMetaFile);

                 try {
                     dataAssembler.loadFiles(obF, metaF,null);
                 } catch (Exception e) {
                     String msg=e.getMessage() ;
                     JOptionPane.showMessageDialog(TsDataLoader.this,msg);

                     if(DEBUG)
                        e.printStackTrace();  //To change body of catch statement use Options | File Templates.
                     return;
                 }
                 dataList.setListData(dataAssembler.getAttributeNameList() );*/
                 try {
                     TsDataLoader.this.loadDataInfo();
                 } catch (Exception e) {
                     if(DEBUG)
                     e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                 }
             }
        };
        dataPathPane.addLoadDataListner(ldl);


    }
    public    boolean loadDataInfo() throws Exception{
                FileLoadingPane flp=dataPathPane ;
                 String obF=flp.getFileLoaderValue(TsDataFile);
                 String metaF=flp.getFileLoaderValue(TsMetaFile);
                 File obf=new File(obF);
                 if(obf.exists() ){
                     return firstProcessFiles(obF,metaF,null);
                 }
                 else{
                     return false;
                 }

    }
    /**
     * Extend by subclass
     * setup listpane
     * @return
     */
    protected   DataSelectPane setupListPane(DataSelectPane  dataP){

        //dataP.setMainSectionName("Time Series");
        dataP.setLeftSectionName("Data available");
        dataP.setRightSectionName("Data for analysis");
        return   dataP;
    }


        public void addTsDataListener(TSDataListener l) {
            dataAssembler.addTsDataListener(l);

      }

      /**
       * removes an DataSetListener from the button
       */
      public void removeTsDataListener(TSDataListener l) {
            dataAssembler.removeTsDataListener(l);
      }
        /*******************************************************************************************************
     * DataEvent
     * Data loaded by PcpDataLoader should have same number of record as that loaded bt TsDataLoader
     * Thus, the dataSource of each DataLoader must listen to each other to keep track the numOfRecord of the other
     * if the numberOfRecords are not equal, must info user
     *******************************************************************************************************/
/*    public void dataSetChanged(DataSetEvent e) {
        AppDataEvent appe=(AppDataEvent) e;
        AppDataModel dataModel=appe.getDataModel() ;
        int numOfRec=dataModel.getNumRecords() ;
        this.dataAssembler.setExpectedNumOfRecord(numOfRec);
    }*/
    
}
